<?php
/**
 * 按订单概览 【点击获取订单时生成拣货单】
 * User: poptser@foxmail.com
 * Date: 2018/4/16
 * Time: 15:58
 */
namespace App\Http\Controllers\Sorting;

use App\Http\Controllers\ApiController;
use App\Services\Sorting\SortingNewService;
use App\Services\Sorting\SortingService;
use App\Services\SortingWeb\OrderService;
use App\Services\SortingWeb\ToolService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SortingNewController extends ApiController{
    public $sortingService;
    public $sortingNewService;
    public $orderService;
    public $toolService;    //辅助工具 service

    public $wid;
    public $userId;
    public $widInfo;
    public $kindeeSn;

    public function __construct(SortingService $sortingService,OrderService $orderService,
                                ToolService $toolService,SortingNewService $sortingNewService){
        $this->sortingService = $sortingService;
        $this->orderService = $orderService;
        $this->toolService = $toolService;
        $this->sortingNewService = $sortingNewService;

        $isLogion = Auth::guard('sorting')->check();
        if(!$isLogion){
            $this->returnErr("api_token 不存在或已过期");
        }else{
            $this->userId = Auth::guard('sorting')->id();
            if($this->checkUserType(1,$this->userId)){
                $this->returnErr("您没有访问此接口得权限");
            }
        }
        $this->wid = $this->toolService->getUserWid($this->userId);
        $this->widInfo = $this->toolService->getWidInfo($this->wid);
        $this->kindeeSn = $this->widInfo->k3_number;
    }

    /**
     * 获取订单公司列表概览
     * Author:poptser@foxmail.com
     * @param Request $request
     * @return mixed
     */
    public function getCompanyOverview(Request $request){
        $showDate = $request->showDate;
        $showDate = date("Y-m-d",strtotime($showDate));
        $companyList = $this->sortingService->getCompanyList();
        //return $companyList;
        $returnData = [];
        foreach ($companyList as $key=>$value){
            $orderNum = $this->sortingNewService->getOrderNum($showDate,$value->id,
                null,null,$this->wid);
            if($orderNum['otherToday']==0&&$orderNum['today']==0){
                unset($companyList[$key]);
            }else{
                $value->orderNum  = $orderNum;
                $returnData[] = $value;
            }
        }

        if(empty($returnData)){
            return $this->success([]);
        }else{
            return $this->success($returnData,"已返回配送公司概览数据");
        }
    }

    /**
     * 获取时间列表概览
     * Author:poptser@foxmail.com
     * @param Request $request
     * @return mixed
     */
    public function getTimeList(Request $request){
        $showDate = $request->showDate;
        $showDate = date("Y-m-d",strtotime($showDate));
        $companyId = $request->companyId;
        if(!isset($companyId)){
            return $this->failedReturn("请选择公司");
        }
        $timeList = $this->sortingNewService->getDistributionTime($showDate,$companyId,null,$this->wid);
        //return $timeList;

        $timeList[] = (object)[
            'best_time'=>"超时订单",
        ];

        foreach ($timeList as $key=>$value){
            if(isset($value->best_time_start)){
                $value->best_time = date("H:i",$value->best_time_start)."-".date("H:i",$value->best_time);
            }
            unset($value->best_time_start);
            $value->orderNum = $this->sortingNewService->getOrderNum($showDate,
                $companyId,$value->best_time,null,$this->wid);
            $value->orderNum['otherToday'] = 0;
        }
        if(empty($timeList)){
            return $this->success([]);
        }else{
            return $this->success($timeList,"已返回时间分段概览数据");
        }
    }

    /**
     * 获取配送划区概览
     * Author:poptser@foxmail.com
     * @param Request $request
     * @return mixed
     */
    public function getZoneList(Request $request){
        $showDate = $request->showDate;
        $companyId = $request->companyId;
        $bestTime = $request->bestTime;
        if(!isset($companyId)){
            return $this->failedReturn("请选择公司");
        }
        if(!isset($bestTime)){
            return $this->failedReturn("请选择时间段");
        }
        $showDate = date("Y-m-d",strtotime($showDate));
        $zoneList = $this->sortingNewService->getDistributionZone($showDate,
            $companyId,$bestTime,$this->wid);
        //return $zoneList;
        foreach ($zoneList as $key=>$value){
            $value->orderNum = $this->sortingNewService->getOrderNum($showDate,
                $companyId,$bestTime,$value->area_id,$this->wid);
            $value->orderNum['otherToday'] = 0;
        }
        if(empty($zoneList)){
            return $this->success([]);
        }else{
            return $this->success($zoneList,"已返回区域概览数据");
        }
    }


    /**
     * 获取一个拣货单
     * Author:poptser@foxmail.com
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function getSortingOrder(Request $request){
        $showDate = $request->showDate;         //日期
        $companyId = $request->companyId;       //配送公司
        $bestTime = $request->bestTime;         //配送时间
        $zoneId = $request->zoneId;             //划区ID
        //return $request;
        $showDate = date("Y-m-d",strtotime($showDate));
        if(empty($showDate)||!is_numeric($companyId)||empty($bestTime)||empty($zoneId)){

            return $this->failedReturn("参数错误");
        }
        $sortingOrder =$this->sortingNewService
            ->setSortingOrder($showDate,$companyId,$bestTime,$zoneId,$this->wid,$this->userId);
        if(empty($sortingOrder)){
            return $this->failedReturn("暂无空闲拣货单");
        }
        $judgment = $this->sortingService->setSortingOrderStatus($sortingOrder->id,$this->userId,3);
        if($judgment===true){
            $ToolService = new ToolService();
            $sortingOrderIdList = $this->orderService->getOrderBySorting($sortingOrder->id);

            $data['sortingSn'] = $sortingOrder->sorting_sn;
            $data['sortingId'] = $sortingOrder->id;
            $data['orderNum'] = count($sortingOrderIdList);
            $data['goodsNum'] = $this->orderService->getGoodsNumByOrder($sortingOrderIdList);
            $data['zone'] = $ToolService->getAreaName($sortingOrder->area_id);
            return $this->success($data,"获取成功");
        }else{
            return $this->failedReturn("获取失败，稍后尝试");
        }
    }
}